﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FormQuanLyTangBan : Form
    {
        public FormQuanLyTangBan()
        {
            InitializeComponent();
        }
        private void LoadCBO()
        {
            TANGDTO TDTO = new TANGDTO();
            TANGBUS TBUS = new TANGBUS();
           DataTable ds1 = new DataTable();
            ds1 = TBUS.DanhSachTangCBO(TDTO);
            cboDSTang.DataSource = ds1;
            cboDSTang.DisplayMember = "MaTang";
            cboDSTang.ValueMember = "MaTang";
        }
        private void btntim_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            TANGBUS TBUS = new TANGBUS();
            TANGDTO TDTO = new TANGDTO();
            TDTO.TenTang = txtTenTang.Text.Trim();
            dt = TBUS.DanhSachTang(TDTO);
            dtgTang.DataSource = dt;
            DemTang();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            TANGDTO TDTO = new TANGDTO();
            string tt = txtTenTangThem.Text.Trim();
            if (tt == "")
            {
                MessageBox.Show("Nhập tên tầng", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            TDTO.TenTang = tt;

            TANGBUS TBUS = new TANGBUS();
            int kq = TBUS.ThemTang(TDTO);
            if (kq > 0)
            {
                MessageBox.Show("Thêm thành công");
                txtTenTangThem.Clear();
                txtTenTangThem.Focus();
                loadsukientang();
                DemTang();
            }
            else
            {
                MessageBox.Show("Thêm thất bại");
                txtTenTangThem.Clear();
                txtTenTangThem.Focus();
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            TANGDTO TDTO = new TANGDTO();
            string mt = txtmatangxoa.Text.Trim();
            if (MessageBox.Show("Bạn có chắc muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            if (mt == "")
            {
                MessageBox.Show("Nhập tầng cần xóa", "Thônng Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            TDTO.MaTang = int.Parse(mt);

            TANGBUS TBUS = new TANGBUS();
            int kq = TBUS.XoaTang(TDTO);
            if (kq > 0)
            {
                MessageBox.Show("Xóa thành công");
                txtmatangxoa.Clear();
                loadsukientang();
                DemTang();
            }
            else
            {
                MessageBox.Show("Xóa thất bại");
            }
        }

        private void SuaTang(string tencu, string ten)
        {
            TANGBUS TBUS = new TANGBUS();
            TANGDTO TDTO = new TANGDTO();
            TDTO.TenTangCu = tencu;
            TDTO.TenTang = ten;

            int kq = TBUS.SuaTang(TDTO);
            if (kq > 0)
            {
                MessageBox.Show("Sửa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                txtTenTangCu.Clear();
                txtTenTangSua.Clear();
                loadsukientang();
            }
            else
            {
                MessageBox.Show("Sửa thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenTangCu.Clear();
                txtTenTangSua.Clear();
                txtTenTangCu.Focus();
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            TANGDTO TDTO = new TANGDTO();
            string ten = txtTenTangSua.Text.Trim();
            string tencu = txtTenTangCu.Text.Trim();
            if (ten == "" || tencu == "")
            {
                MessageBox.Show("Nhập tầng muốn sửa", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenTang.Focus();
                return;
            }
            else
            {
                SuaTang(tencu, ten);
            }
        }
        //Kiểm tra dữ liệu nhập vào có phải chuỗi//
        internal static bool IsNumeric(string numberString)
        {
            foreach (char c in numberString)
            {
                if (!char.IsNumber(c))
                    return false;
            }
            return true;
        }

        private void btnTimBan_Click(object sender, EventArgs e)
        {
           
            DataTable dt = new DataTable();
            BANBUS BBUS = new BANBUS();
            BANDTO BDTO = new BANDTO();
            string tb = txtTenBanTim.Text.Trim();
            string sc;
            sc = txtSucChua.Text.Trim();
            if (sc == "")
            {
                MessageBox.Show("Nhập thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            BDTO.TenBan = tb;
            BDTO.SucChua = int.Parse(sc);
            dt = BBUS.TimBan(BDTO);
            dtgBan.DataSource = dt;
            DemBan();
        }
                
        private void btnThemBan_Click(object sender, EventArgs e)
        {
           
            BANDTO BDTO = new BANDTO();
            string tb = txtTenBanTang.Text.Trim();
            string sc = txtSucChuaThem.Text.Trim();
            string tang = cboDSTang.Text;

           
            if (tb == "" || sc == ""||tang=="")
            {
                MessageBox.Show("Nhập thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            BDTO.MaTang = int.Parse(tang);
            BDTO.TenBan = tb;
            BDTO.SucChua = int.Parse(sc);

            BANBUS BBUS = new BANBUS();
            int kq = BBUS.ThemBan(BDTO);
            if (kq > 0)
            {
                MessageBox.Show("Thêm thành công");
                txtTenBanTang.Clear();
                txtSucChuaThem.Clear();
                loadsukienban();
                DemBan();
            }
            else
            {
                MessageBox.Show("Thêm thất bại");
                txtTenBanTang.Clear();
                txtSucChuaThem.Clear();
            }
        }

        private void btnXoaBan_Click(object sender, EventArgs e)
        {
            BANDTO BDTO = new BANDTO();
            string mb = txtMaBanXoa.Text.Trim();
            if (MessageBox.Show("Bạn có chắc muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            if (mb == "")
            {
                MessageBox.Show("Nhập bàn cần xóa", "Thônng Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            BDTO.MaBan = int.Parse(mb);

            BANBUS BBUS = new BANBUS();
            int kq = BBUS.XoaBan(BDTO);
            if (kq > 0)
            {
                MessageBox.Show("Xóa thành công");
                txtMaBanXoa.Clear();
                txtMaBanXoa.Focus();
                loadsukienban();
                DemBan();
            }
            else
            {
                MessageBox.Show("Xóa thất bại");
            }
        }

        private void btnCTHD_Click(object sender, EventArgs e)
        {
           
            DataTable dt = new DataTable();
            BANBUS BBUS = new BANBUS();
            BANDTO BDTO = new BANDTO();        

            string mb = txtMaBanCTHD.Text.Trim();
            
            if (mb == "")
            {
                MessageBox.Show("Nhập thông tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            BDTO.MaBan = int.Parse(mb);
            dt = BBUS.ChiTietHD(BDTO);
            dtgBan.DataSource = dt;
        }

        private void dtgTang_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtTenTang.Text = dtgTang.CurrentRow.Cells["TenTang"].Value.ToString();
            txtmatangxoa.Text = dtgTang.CurrentRow.Cells["MaTang"].Value.ToString();
            txtTenTangCu.Text = dtgTang.CurrentRow.Cells["TenTang"].Value.ToString();
        }
        private void SuaBan(string tenbancu, string tenban, int succhua)
        {
            BANBUS BBUS = new BANBUS();
            BANDTO BDTO = new BANDTO();
            BDTO.TenBanCu = tenbancu;
            BDTO.TenBan = tenban;
            BDTO.SucChua = succhua;

            int kq = BBUS.SuaBan(BDTO);
            if (kq > 0)
            {
                MessageBox.Show("Sửa thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                txtTenBanCanSua.Clear();
                txtTenBanCanSua.Focus();
                txtTenBanSua.Clear();
                txtSucChuaSua.Clear();
                loadsukienban();
            }
            else
            {
                MessageBox.Show("Sửa thất bại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenBanCanSua.Clear();
                txtTenBanCanSua.Focus();
                txtTenBanSua.Clear();
                txtSucChuaSua.Clear();
            }
        }
        private void btnSuaBan_Click(object sender, EventArgs e)
        {
           
            int succhua;
            BANDTO BDTO = new BANDTO();
            string tenbancu = txtTenBanCanSua.Text.Trim();
            string tenban = txtTenBanSua.Text.Trim();
            succhua = int.Parse(txtSucChuaSua.Text.Trim());

            if (tenban == "" || tenbancu == "" || succhua == 0)
            {
                MessageBox.Show("Nhập thông tin muốn sửa", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenBanCanSua.Clear();
                txtTenBanCanSua.Focus();
                txtTenBanSua.Clear();
                txtSucChuaSua.Clear();
                return;
            }
            else
            {
                SuaBan(tenbancu, tenban, succhua);
            }
        }

        private void dtgBan_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtSucChua.Text = dtgBan.CurrentRow.Cells["SucChua"].Value.ToString();
            txtTenBanCanSua.Text = dtgBan.CurrentRow.Cells["TenBan"].Value.ToString();
            txtTenBanTim.Text = dtgBan.CurrentRow.Cells["TenBan"].Value.ToString();
            txtMaBanXoa.Text = dtgBan.CurrentRow.Cells["MaBan"].Value.ToString();
            txtMaBanCTHD.Text = dtgBan.CurrentRow.Cells["MaBan"].Value.ToString();
            
        }
        private void loadtang()
        {
            DataTable dt = new DataTable();
            TANGBUS TBUS = new TANGBUS();
            TANGDTO TDTO = new TANGDTO();
            dt = TBUS.DanhSachTangFull(TDTO);
            dtgTang.DataSource = dt;
        }
        private void loadsukientang()
        {
            DataTable dt = new DataTable();
            TANGBUS TBUS = new TANGBUS();
            TANGDTO TDTO = new TANGDTO();
            dt = TBUS.LOADTANG(TDTO);
            dtgTang.DataSource = dt;
        }

        private void FormQuanLyTangBan_Load(object sender, EventArgs e)
        {
            txtTenTang.Focus();
            txtTenBanTim.Focus();
            loadsukientang();
            loadsukienban();
            LoadCBO();
            DemTang();
            DemBan();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
         private void loadban()
        {

            DataTable dt = new DataTable();
            BANBUS BBUS = new BANBUS();
            BANDTO BDTO = new BANDTO();
            dt = BBUS.TimBanFull(BDTO);
            dtgBan.DataSource = dt;
        }
         private void loadsukienban()
         {

             DataTable dt = new DataTable();
             BANBUS BBUS = new BANBUS();
             BANDTO BDTO = new BANDTO();
             dt = BBUS.LOADBAN(BDTO);
             dtgBan.DataSource = dt;
         }

         private void txtMaTangThem_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void txtTenBanTim_KeyPress(object sender, KeyPressEventArgs e)
         {

         }

         private void txtSucChua_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void txtSucChuaThem_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void txtSucChua_TextChanged(object sender, EventArgs e)
         {

         }

         private void txttentangxoa_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnxoa_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenTangThem_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnthem_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenTangSua_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnSua_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void tabcontrol1_KeyDown(object sender, KeyEventArgs e)
         {

         }

         private void txtTenTang_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btntim_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanTim_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnTimBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtSucChua_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnTimBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtMaTangThem_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnThemBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanTang_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnThemBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtSucChuaThem_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnThemBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanXoa_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnXoaBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanCanSua_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnSuaBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanSua_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnSuaBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtSucChuaSua_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnSuaBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtMaBanCTHD_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnCTHD_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenBanTim_TabIndexChanged(object sender, EventArgs e)
         {
             
         }
         private void DemTang()
         {
             int tong;
             tong = dtgTang.RowCount;

             txtTongTang.Text = tong.ToString();
         }
         private void DemBan()
         {
             int tong;
             tong = dtgBan.RowCount;

             txtTongBan.Text = tong.ToString();
         }

         private void txtMaBanCTHD_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void txtmatangxoa_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void txtMaBanXoa_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void cboDSTang_KeyPress(object sender, KeyPressEventArgs e)
         {
             if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
             {
                 e.Handled = true;
             }
         }

         private void cboDSTang_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnThemBan_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }

         private void txtTenTangCu_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.Enter)
             {
                 btnSua_Click(sender, e);
             }
             else if (e.KeyCode == Keys.Escape)
             {
                 this.Close();
             }
         }
    }
}
